<template>
  <view class="container">
    <view class="form">
      <text class="title">用户注册</text>
      <input class="input" type="text" placeholder="请输入用户名" v-model="username" placeholder-style="color:#999"/>
      <input class="input" placeholder="请输入密码" v-model="password" type="password" placeholder-style="color:#999"/>
      <input class="input" placeholder="请确认密码" v-model="confirmPassword" type="password" placeholder-style="color:#999"/>
      <button class="btn" type="primary" @click="handleRegister">注册</button>
      <view class="link">
        <text>已有账号？</text>
        <navigator url="/pages/Login/Login" class="login-text">去登录</navigator>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      username: '',
      password: '',
      confirmPassword: ''
    }
  },
  methods: {
    handleRegister() {
      if (!this.username || !this.password || !this.confirmPassword) {
        uni.showToast({
          title: '请填写完整信息',
          icon: 'none'
        });
        return;
      }
      if (this.password !== this.confirmPassword) {
        uni.showToast({
          title: '两次密码不一致',
          icon: 'none'
        });
        return;
      }
	  uniCloud.callFunction({
	  	name:"addUser",
		data:{
			username:this.username,
			password:this.password
		},
		success: (nsg) => {
			console.log("addUser CallBack"+JSON.stringify(msg));
			setTimeout(()=>{
				uni.hideLoading()
				uni.showToast({
					title:'注册成功',
					icon:'success'
				});
			},1000);
			setTimeout(()=>{
				uni.navigateTo({
					url: '/pages/Login/Login'
				})
			},2000);
		},
		fail: (res) => {
			console.log("addUser调用失败："+JSON.stringify(res));
			uni.hideLoading();
			uni.showToast({
				title:'注册失败',
				icon:'nono'
			});
		}
	  });
      uni.showToast({
        title: '注册成功',
        icon: 'success'
      });
        uni.navigateTo({
          url: '/pages/Login/Login'
        });
    }
  }
}
</script>

<style scoped>
.container {
  width: 100%;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 30rpx;
  box-sizing: border-box;
  background-color: #f8f8f8;
}
.form {
  width: 100%;
  max-width: 600rpx;
}
.title {
  display: block;
  text-align: center;
  font-size: 44rpx;
  font-weight: 600;
  color: #333;
  margin-bottom: 60rpx;
}
.input {
  color: #333;
  font-size: 30rpx;
  padding: 30rpx 24rpx;
  margin-bottom: 36rpx;
  border: 2rpx solid #eee;
  background-color: #fff;
  border-radius: 12rpx;
}
.login-text {
  color: #007aff;
  margin-left: 12rpx;
}
.link {
  text-align: center;
  font-size: 26rpx;
  color: #666;
}
.btn {
  width: 100%;
  height: 96rpx;
  font-size: 32rpx;
  border-radius: 48rpx !important;
  margin-bottom: 40rpx;
}
</style>